package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.mapper.EmployeeMapper;
import com.itheima.reggie.mapper.OrderMapper;
import com.itheima.reggie.pojo.*;
import com.itheima.reggie.service.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper,Orders>implements OrderService {

    @Autowired
    private ShoppingCartService shoppingCartService;
    @Autowired
    private UserService userService;
    @Autowired
    private AddressBookService addressBookService;
    @Autowired
    private OrderDetailService orderDetailService;
    //购物车->订单->订单明细
    @Override
    @Transactional
    public void submitOrders(Orders orders) {
        //查询购物车集合
        LambdaQueryWrapper<ShoppingCart> shoppingCartLambdaQueryWrapper = new LambdaQueryWrapper<>();
        Long user_Id = BaseContext.getCurrentId();
        shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getUserId,user_Id);
        List<ShoppingCart> Cartlist = shoppingCartService.list(shoppingCartLambdaQueryWrapper);

        //设置oreder表
        String number = IdWorker.get32UUID();
        int status=2;
        LocalDateTime order_time = LocalDateTime.now();
        LocalDateTime chechout_time = LocalDateTime.now();
        //拿到总金额数
        BigDecimal bigDecimal = new BigDecimal(0);
        for (ShoppingCart shoppingCart : Cartlist) {
            BigDecimal p1 = shoppingCart.getAmount();
            BigDecimal count = new BigDecimal(shoppingCart.getNumber());
            BigDecimal sum = p1.multiply(count);
            bigDecimal=bigDecimal.add(sum);
        }
        User user = userService.getById(user_Id);
        String phone = user.getPhone();
        AddressBook addressBook = addressBookService.getById(orders.getAddressBookId());
        String address = addressBook.getDetail();
        String userName = user.getName();
        String consignee = addressBook.getConsignee();
        orders.setNumber(number);
        orders.setStatus(status);
        orders.setUserId(user_Id);
        orders.setOrderTime(order_time);
        orders.setCheckoutTime(chechout_time);
        orders.setAmount(bigDecimal);
        orders.setCheckoutTime(chechout_time);
        orders.setPhone(phone);
        orders.setAddress(address);
        orders.setUserName(userName);
        orders.setConsignee(consignee);
        super.save(orders);//保存后就自动生成了orderId
        List<OrderDetail> orderDetailList = Cartlist.stream().map(shoppingCart -> {
            OrderDetail orderDetail = new OrderDetail();
            BeanUtils.copyProperties(shoppingCart, orderDetail, "id");//id自动生成
            orderDetail.setOrderId(orders.getId());
            return orderDetail;
        }).collect(Collectors.toList());
        orderDetailService.saveBatch(orderDetailList);
        shoppingCartService.remove(shoppingCartLambdaQueryWrapper);
    }
}
